#!/usr/bin/env bash

### settings
# www/
# settings_and_scripts
# disks
#    data/ ; backup/ ; download/ ;
BinHome="/opt/server.lan"
MntHome="/mnt/server.lan"
TmpHome="/tmp/server.lan"
#########################################
dataStorage=$BinHome/disks/data/dataStorage
backupStorage=$BinHome/disks/backup/backupStorage
downloadStorage=$BinHome/disks/download/downloadStorage
#########################################

echo "

[INFO] date:: `date`"

echo "[INFO] checker::START"

#  /ect/rc.local
# bash /usr/server.lan/rc.local_boot.sh &
#####################################################

# 0.1 check
#### check bin
# http_server
if [ ! -e "${BinHome}/http_server" ];then
  echo "[Error] please install http_server" >&2
  exit 1
elif [ ! -x "${BinHome}/http_server" ];then
  chmod u+x "${BinHome}/http_server"
fi
# check docker
echo "[INFO] software requirement checker"
if ! which "docker";then
    echo "[Warn] please install docker" >&2
fi

#  borgbackup
flagAptUpdated=false
aptUpdate(){
  if $flagAptUpdated;then
    return
  fi

  apt update -y 
  flagAptUpdated=true

}

function aptInstall(){
  if ! which "$1";then
    aptUpdate
    apt install "$1" -y
  fi
}
aptInstall borgbackup
aptInstall rsync


#### check tls
echo "[INFO] ssl key & crt checker"
serverCrt="${BinHome}/server.crt"
serverKey="${BinHome}/server.key"

if [ ! -e $serverCrt ] || [ ! -e $serverKey ];then
  echo "[E] server tls key can't find error" >&2
  exit 2
fi

#### check mountpoint
# stop docker before disk(data) mount check
checkMountpointAtBootTime(){
  if [ -e "$BinHome/disks/NO_CHECK_MOUNTPOINT" ];then
    return
  fi

  
  service docker stop || true

  echo "
  **** ****
  [INFO] check Mountpoint"
  {
  flag=false
  for i in 1 2 3 4 5 6 7 8 9 10;do
    if $flag;then
      break
    fi
    echo "`date` check mountpoint:: i=$i"

    if [ $i -eq 10 ];then
      echo "[E] mountpoint error, exit" 
      exit 3
    fi

    for j in 2 3 4 5 6 7 8 9 10;do
      echo "    check mountpoint:: i,j=$i,$j"
      if  mountpoint "$(dirname "$dataStorage")" -q ;then
        flag=true
        break
      fi
      mount -a
      sleep $((i*j*j))
    done
  done
  } | tee "/tmp/http_server_boot_time.log"
  echo "[INFO] done :: check Mountpoint
  **** **** **** 
  "
}

checkMountpointAtBootTime


echo "[INFO] checker::DONE


"

#########
mkdirchmod(){
  if [ ! -e "$1" ];then
    echo "[INFO] mkdir and chmod :: $1"
    mkdir -p "$1"
    chown 1000:1000 "$1"
  fi
}

# 0.2
# for server_Lan
echo "[INFO] mkdir and chmod if not exists (for server_Lan)"
mkdirchmod "${dataStorage}/server.lan"
mkdirchmod "${dataStorage}/server.lan/upload"

if [ ! -e "${dataStorage}/server.lan/log" ];then
  mkdirchmod "${dataStorage}/server.lan/log"
  date >> "${dataStorage}/server.lan/log/d.log"
fi


# 0.3
mkdirchmod "$TmpHome"

# "${BinHome}/tmp" -> $TmpHome
## for write log/tmp data
echo "[INFO] make link  for $TmpHome"
if [ ! -e "${BinHome}/tmp" ] ;then
  ln -sf "$TmpHome" "${BinHome}/tmp"
fi
## for file server
if [ ! -e "${BinHome}/www/data/tmp" ];then
  ln -sf "$TmpHome" "${BinHome}/www/data/tmp"
fi
###



# start services
echo "[INFO] restart docker service"
service docker restart || service docker start 
echo ""

# start http_server
echo "[INFO] start http_server"
{
  ${BinHome}/http_server &
  pid=$!
  echo "$pid" > "$TmpHome/pid"
} | tee $TmpHome/http_server.log &



###################
